package com.mazaiting.classfile.attr.info;

import com.mazaiting.classfile.ClassReader;
import com.mazaiting.classfile.attr.entity.LocalVariableTableEntity;
import com.mazaiting.classfile.attr.entity.LocalVariableTypeTableEntity;
import com.mazaiting.classfile.constant.ConstantPool;

import java.util.Arrays;

/**
 * 局部变量类型表属性
 *
 * @author mazaiting
 * @since 2021/8/13
 */
public class LocalVariableTypeTableAttribute extends AttributeInfo{
    /**
     * 局部变量表类型长度 无符号 u2
     */
    private int localVariableTypeTableLength;
    /**
     * 局部变量表实体数组
     */
    private LocalVariableTypeTableEntity[] localVariableTableTableEntities;

    public LocalVariableTypeTableAttribute(ConstantPool pool, int index, int length) {
        super(pool, index, length);
    }

    @Override
    public void readInfo(ClassReader reader) {
        this.localVariableTypeTableLength = reader.readU2ToUnsignedShort();
        this.localVariableTableTableEntities = new LocalVariableTypeTableEntity[localVariableTypeTableLength];
        for (int i = 0; i < localVariableTypeTableLength; i++) {
            this.localVariableTableTableEntities[i] = new LocalVariableTypeTableEntity(
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort()
            );
        }
    }

    @Override
    public String toString() {
        return "LocalVariableTypeTableAttribute{" +
                "nameIndex=" + nameIndex +
                ", length=" + length +
                ", localVariableTypeTableLength=" + localVariableTypeTableLength +
                ", localVariableTableTableEntities=" + Arrays.toString(localVariableTableTableEntities) +
                '}';
    }
}


















